package com.msb.security.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

/**
 * @author sunpeng
 * @Date 2021-08-26 11:04
 */

@Service
public class SecurityService implements UserDetailsService {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        if(!username.equals("admin")){
            throw new UsernameNotFoundException("未发现");
        }
        String password = "pwd";
        String encode = passwordEncoder.encode(password);
        System.out.println(encode);
        UserDetails userDetails = new User(username, encode, AuthorityUtils.commaSeparatedStringToAuthorityList("admin1,admin2"));
        return userDetails;
    }
}
